package com.huawei.itemservice.service.impl;

import com.huawei.common.pojo.Result;
import com.huawei.common.utils.SnowflakeIdWorker;
import com.huawei.common.utils.ThreadLocalUtil;
import com.huawei.itemservice.mapper.ItemMapper;
import com.huawei.itemservice.pojo.dto.ItemDTO;
import com.huawei.itemservice.pojo.entity.Item;
import com.huawei.itemservice.pojo.vo.ItemVO;
import com.huawei.itemservice.properties.SnowFlakeProperties;
import com.huawei.itemservice.service.ItemService;
import com.huawei.itemservice.utils.MinioUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;


@Service
public class ItemServiceImpl implements ItemService {

    @Autowired
    private ItemMapper itemMapper;
    @Autowired
    private SnowFlakeProperties snowFlakeProperties;
    @Autowired
    private MinioUtils minioUtils;



    @Override
    public Result createItem(ItemDTO itemdto) {
        Item item = new Item();
        BeanUtils.copyProperties(itemdto,item);
        item.setCreateTime(LocalDateTime.now());
        item.setUpdateTime(LocalDateTime.now());
        String[] userInfo = ThreadLocalUtil.get();
        item.setCreateUser(Long.parseLong(userInfo[0]));
        SnowflakeIdWorker idWorker = new SnowflakeIdWorker(snowFlakeProperties.getWorkId(), snowFlakeProperties.getDataCenterId());
        long id = idWorker.nextId();
        item.setId(id);
        itemMapper.createItem(item);
        return new Result(0,"菜品上传成功！",null);
    }

    @Override
    public Result uploadProfile(MultipartFile profile) {
        if(profile == null){
            return Result.error("上传文件为空");
        }else{
            String url = minioUtils.upload(profile);
            return new Result(0,"上传成功",url);
        }
    }

    @Override
    public Result updateItem(ItemDTO itemdto) {
        Item item = new Item();
        BeanUtils.copyProperties(itemdto,item);
        item.setUpdateTime(LocalDateTime.now());
        itemMapper.updateItem(item);
        return new Result(0,"修改商品信息成功",null);
    }

    @Override
    public Result deleteItem(Long id) {
        itemMapper.deleteItem(id);
        return new Result(0,"删除菜品成功",null);
    }

    @Override
    public Result getItemById(Long id) {
        Item item = itemMapper.getItemById(id);
        if(item == null){
            return Result.error("菜品不存在");
        }
        ItemVO itemVO = new ItemVO();
        BeanUtils.copyProperties(item,itemVO);
        return new Result(0,"查询菜品成功",itemVO);
    }

    @Override
    public Result findAllItems() {
        String[] userInfo = ThreadLocalUtil.get();
        List<Long> ids = itemMapper.findAllItems(Long.parseLong(userInfo[0]));
        return new Result(0,"所有菜品id查询成功",ids);
    }

    @Override
    public Result findByIds(List<Long> ids) {
        List<Item> items = new ArrayList<>();
        for(Long id : ids){
            items.add(itemMapper.getItemById(id));
        }
        return new Result(0,"批量查询成功",items);
    }

    @Override
    public Result getAllItems() {
        List<Item> items = new ArrayList<>();
        String[] userInfo = ThreadLocalUtil.get();
        Long id = Long.parseLong(userInfo[0]);
        List<Long> ids = itemMapper.getAllItems(id);
        for(Long itemId : ids){
            Item item = itemMapper.getItemById(itemId);
            items.add(item);
        }
        return new Result(0,"获取所有菜品成功",items);
    }
}
